jQuery.extend({
	/**
	 * @see  将json字符串转换为对象
	 * @param   json字符串
	 * @return 返回object,array,string等对象
	 */
	evalJSON : function(strJson) {
		return eval("(" + strJson + ")");
	}
});

jQuery.extend({
	/**
	 * @see  将javascript数据类型转换为json字符串
	 * @param 待转换对象,支持object,array,string,function,number,boolean,regexp
	 * @return 返回json字符串
	 */
	toJSONString : function(object) {
		var type = typeof object;
		if ('object' == type) {
			if (object==null)
				type = 'object';
			else if (Array == (object.constructor||""))
				type = 'array';
			else if (RegExp == (object.constructor||""))
				type = 'regexp';
			else
				type = 'object';
		}
		switch (type) {
		case 'undefined':
		case 'unknown':
			return;
			break;
		case 'function':
		case 'boolean':
		case 'regexp':
			return object.toString();
			break;
		case 'number':
			return isFinite(object) ? object.toString() : 'null';
			break;
		case 'string':
			return '"'
					+ object.replace(/(\\|\")/g, "\\$1").replace(
							/\n|\r|\t/g,
							function() {
								var a = arguments[0];
								return (a == '\n') ? '\\n' : (a == '\r') ? '\\r' : (a == '\t') ? '\\t' : ""
							}) + '"';
			break;
		case 'object':
			if (object === null)
				return 'null';
			var results = [];
			for ( var property in object) {
				var value = jQuery.toJSONString(object[property]);
				if (value !== undefined)
					results.push(jQuery.toJSONString(property) + ':' + value);
			}
			return '{' + results.join(',') + '}';
			break;
		case 'array':
			var results = [];
			for ( var i = 0; i < object.length; i++) {
				var value = jQuery.toJSONString(object[i]);
				if (value !== undefined)
					results.push(value);
			}
			return '[' + results.join(',') + ']';
			break;
		}
	}
});


/**
 * 相对于serialize方法，将表单序列化为一个“&”连接的字符串，
 * serializeJson方法将表单转化成Json对象 
 * @param joinSame 是否将同名参数连接起来，默认用","
 * @param joinDelim 指定同名参数连接的连接符，默认为","
 */
(function($) {
	$.fn.serializeJson = function(joinSame, joinDelim) {
		var serializeObj = {};
		var array = this.serializeArray();
		var str = this.serialize();
		joinDelim = joinDelim || ",";
		$(array).each(function() {
			    if (this.name in serializeObj) {
			    	if(joinSame){
			    		serializeObj[this.name] = serializeObj[this.name] + joinDelim + this.value;
			    	}else{
					    if ($.isArray(serializeObj[this.name])) {
						    serializeObj[this.name].push(this.value);
					    } else {
						    serializeObj[this.name] = [serializeObj[this.name], this.value];
					    }
			    	}
			    } else {
				    serializeObj[this.name] = this.value;
			    }
		    });
		return serializeObj;
	};
})(jQuery);